package com.itheima.controller;

import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author chencfgs
 * @date 2019-12-06 15:23
 */

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/send4Order")
    public Result send4Order(String telephone, HttpServletRequest request) {
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        try {

            //判断同一用户的请求时间是否超过1分钟
            String ip = request.getRemoteAddr();
            Date date = new Date();
            long currentTime = date.getTime();
            String s = jedisPool.getResource().get(ip);
            long redisTime ;
            if ("".equals(s)){
                redisTime = 0;
            }else{
                redisTime = Long.parseLong(jedisPool.getResource().get(telephone));
            }

            if (currentTime-redisTime>60*30){
                //如果已经超过一分钟
                SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, code.toString());
                long dateTime = new Date().getTime();
                jedisPool.getResource().set(ip,String.valueOf(dateTime));
            }else{
                return new Result(false,"同一用户一分钟内不能重复发");
            }

        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        System.out.println("发送的手机验证码为：" + code);
        jedisPool.getResource().setex(telephone + RedisMessageConstant.SENDTYPE_ORDER, 30 * 60, code.toString());
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

    @RequestMapping("/send4Login")
    public Result send4Login(String telephone,HttpServletRequest request) {
        Integer code = ValidateCodeUtils.generateValidateCode(6);
        try {

            //判断同一用户的请求时间是否超过1分钟
            String ip = request.getRemoteAddr();
            Date date = new Date();
            long currentTime = date.getTime();
            String s = jedisPool.getResource().get(ip);
            long redisTime ;
            if ("".equals(s)){
                redisTime = 0;
            }else{
                redisTime = Long.parseLong(jedisPool.getResource().get(telephone));
            }

            if (currentTime-redisTime>60*30){
                //如果已经超过一分钟
                SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, code.toString());
                long dateTime = new Date().getTime();
                jedisPool.getResource().set(ip,String.valueOf(dateTime));
            }else{
                return new Result(false,"同一用户一分钟内不能重复发");
            }


        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        System.out.println("发送的手机验证码为：" + code);
        jedisPool.getResource().setex(telephone + RedisMessageConstant.SENDTYPE_ORDER, 30 * 60, code.toString());
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
}
